// pages/productDetail/index.js
//1. 数据库初始化
var db = wx.cloud.database();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 商品数据
    product: {},
    isLike: true,
    // banner
    imgUrls: [
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg"
    ],
    indicatorDots: true, //是否显示面板指示点
    autoplay: true, //是否自动切换
    interval: 3000, //自动切换时间间隔,3s
    duration: 1000, //  滑动动画时长1s

    // 商品详情介绍
    detailImg: [
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
      "https://img.alicdn.com/imgextra/i3/6000000004178/O1CN01j16xdT1gjZcs5DiTH_!!6000000004178-0-octopus.jpg",
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var pid = decodeURIComponent((options.pid))
    db.collection("product").doc(pid)
    .get({
      //success:查询成功的回调函数   res:返回的结果
      success:res=>{
        console.log(res);
        this.setData({
          product:res.data
        })
        console.log(this.data.product);
        this.getproductdetail()
      }
    })
  },

  /**
   * 获取详情信息
   */
  getproductdetail(){
    console.log(this.data.product._id);
    if(this.data.product._id!=-1){
      var id = this.data.product._id + ""
      console.log(id);
      db.collection("productdetail").where({
        productId:id
      }).get({
        success:res=>{
          console.log(res.data[0]);
          this.setData({
            imgUrls:res.data[0].imgUrls,
            detailImg:res.data[0].detailImg
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
    //预览图片
    previewImage: function (e) {
      var current = e.target.dataset.src;
  
      wx.previewImage({
        current: current, // 当前显示图片的http链接  
        urls: this.data.imgUrls // 需要预览的图片http链接列表  
      })
    },
    // 收藏
    addLike() {
      this.setData({
        isLike: !this.data.isLike
      });
    },
    // 跳到购物车
    toCar() {
      wx.switchTab({
        url: '/pages/cart/cart'
      })
    },
    // 立即购买
    immeBuy() {
      wx.showToast({
        title: '购买成功',
        icon: 'success',
        duration: 2000
      });
    }
})